package com.wzq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Image;
import com.wzq.dao.pojo.Result;
import com.wzq.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private IImageService imageService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Image image) {
        boolean flag = imageService.save(image);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Image image) {
        boolean flag = imageService.updateById(image);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
    //    根据项目id 获取图片
    @GetMapping("/{p_id}")
    public Result getById(@PathVariable Integer p_id) {
        List<Image> list = imageService.list(new LambdaQueryWrapper<Image>().eq(Image::getP_id, p_id));
        if (list!=null){
            return new Result(Code.GET_OK,list);
        }
        return new Result(Code.GET_ERR,null);
    }
    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = imageService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Image> list = imageService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

}
